# Makefile for building the Cloudisk server

# Compiler and flags
CC      := gcc
CFLAGS  := -Wall -Wextra -g -I.
LDLIBS  := -ll8w8jwt -lmysqlclient -lpthread -lcrypt

# Output binary
TARGET      := cloudisk_server
BUILD_DIR   := build

# Collect all C sources under this directory (excluding tests)
SRCS := $(shell find . -name '*.c' -not -path './tests/*')
SRCS := $(patsubst ./%,%,$(SRCS))
OBJS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(SRCS))
DEPS := $(OBJS:.o=.d)

.PHONY: all clean run

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS)

$(BUILD_DIR)/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -MMD -MP -c $< -o $@

-include $(DEPS)

run: $(TARGET)
	./$(TARGET)

clean:
	rm -rf $(BUILD_DIR) $(TARGET)
